package com.centaline.androidsalesblog.fragments.list;

import android.content.Intent;
import android.view.View;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.sale.RentDetailActivity;
import com.centaline.androidsalesblog.adapter.RentListAdapter;
import com.centaline.androidsalesblog.api.sale.PostListApi;
import com.centaline.androidsalesblog.base.BaseFragment;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.model.responseobject.PostListRo;
import com.centaline.androidsalesblog.widgets.MRecyclerView;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IRecycleViewListener;
import com.centanet.framework.interfaces.IResponseListener;

import java.util.ArrayList;

/**
 * 经纪人名下租房
 * Created by guilin on 16/1/26.
 */
public class RentOfStaffFragment extends BaseFragment {

    private MRecyclerView mRecyclerView;

    private ArrayList<PostDo> list = new ArrayList<>();
    private int iRefreshType = 1;
    private PostListApi postListApi;
    private boolean hasRequest;

    @Override
    protected int setLayoutId() {
        return R.layout.layout_m_recyclerview;
    }

    @Override
    protected void findViews(View view) {
        mRecyclerView = (MRecyclerView) view.findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void initView() {
        mRecyclerView.setIRecycleViewListener(new IRecycleViewListener() {
            @Override
            public void downRefresh() {
                hasRequest = true;
                iRefreshType = 1;
                postListApi.setStartIndex(0);
                request();
            }

            @Override
            public void upRefresh() {
                iRefreshType = 2;
                postListApi.setStartIndex(list.size());
                request();
            }

            @Override
            public void onItemClick(int position) {
                startActivity(new Intent(getActivity(), RentDetailActivity.class)
                        .putExtra(ExtraConstant.PostId, list.get(position).getPostId()));
            }
        });
        mRecyclerView.setAdapter(new RentListAdapter(getActivity(), GlideLoad.init(this), list));

        postListApi = new PostListApi(RentOfStaffFragment.class, PostListRo.class, new IResponseListener<PostListRo>() {
            @Override
            public void onResponse(PostListRo postListRo) {
                stopRefresh(postListRo.getList());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                refreshError();
            }
        });
        postListApi.setPostType("r");
        postListApi.setStaffNo(getArguments().getString(ExtraConstant.StaffNo));
    }

    @Override
    protected void loadData() {
        if (!hasRequest) {
            mRecyclerView.startRefresh();
        }
    }

    private void request() {
        postListApi.startRequest();
    }

    /**
     * 开启下拉刷新
     */
    public void startRefresh() {
        mRecyclerView.startRefresh();
    }

    /**
     * 停止刷新
     */
    public void stopRefresh(ArrayList<PostDo> arrayList) {
        if (iRefreshType == 1) {
            list.clear();
        }
        if (arrayList == null) {
            mRecyclerView.stopRefresh(false);
        } else {
            list.addAll(arrayList);
            mRecyclerView.stopRefresh(arrayList.size() == 10);
        }
    }

    public void refreshError() {
        mRecyclerView.refreshError();
    }
}
